<?php

class Core_Model_Subject_Content extends Dcmp_Model_Tag {

    public function __construct() {
        parent::__construct();
        $this->tagType = 'subject';
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_subject_content');
    }

    public function getAccountRelation(){
         $accountId = array();
         $accounts = App::getModel('permission/collection_account')
	    			->innerJoin('{t}subject_account', 'g', 'g.account_id', 'a.id')
                                ->field('g.subject_id','subject_id')
	    			->eq('subject_id',$this->getId())->load();
         foreach($accounts as $account){
             $accountId[] = $account->getId();
         }
         return $accountId;
    }

    public function getAccount(){
        $account  = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}permission_account','e')
                        ->fields('e.*')
                        ->innerJoin('{t}subject_account','f',$this->getId(),'f.subject_id')
                        ->load();
        return $account;
    }

    public function updateClickCount() {
        Kd_Data_Factory::getUpdateDcl()
                ->into('{t}subject_content')
                ->value('click_count', Kd_Data_Dcl_Fn::addition('click_count', 1))
                ->eq('id', ':id:i')
                ->addParameter('id', $this->getId())
                ->execute();
        return $this;
    }

    public function assignRoad($roadId, $startTime, $endTime, $assignerId, $transaction = NULL) {
        $this->deleteRoad($roadId, $transaction);
        Kd_Data_Factory::getInsertDcl()
                ->into('{t}subject_road')
                ->value('subject_id', $this->getId())
                ->value('assigner_id', ':assignerId:i')
                ->value('is_assigned', 1)
                ->value('assigned_time', ':assignedTime:s')
                ->value('assign_start_time', ':assignStartTime:s')
                ->value('assign_end_time', ':assignEndTime:s')
                ->value('road_id', ':roadId:s')
                ->addParameter(array('assignerId' => $assignerId,
                    'assignedTime' => date('Y-m-d H:i:s'),
                    'assignStartTime' => $startTime,
                    'assignEndTime' => $endTime,
                    'roadId' => $roadId))
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
        return $this;
    }

    public function deleteRoad($road, $transaction = NULL) {
        $roads = is_array($road) ? $road : array($road);
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}subject_road')
                ->where()
                ->eq('subject_id', ':subjectId:i')
                ->contain('road_id', ':roadId:s')
                ->closest()
                ->addParameter('subjectId', $this->getId())
                ->addParameter('roadId', $roads)
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();

        return $this;
    }

    public function assignIndustry($industryId, $startTime, $endTime, $assignerId, $transaction = NULL) {
        $this->deleteIndustry($industryId, $transaction);
        Kd_Data_Factory::getInsertDcl()
                ->into('{t}subject_industry')
                ->value('subject_id', $this->getId())
                ->value('assigner_id', ':assignerId:i')
                ->value('is_assigned', 1)
                ->value('assigned_time', ':assignedTime:s')
                ->value('assign_start_time', ':assignStartTime:s')
                ->value('assign_end_time', ':assignEndTime:s')
                ->value('industry_id', ':industryId:s')
                ->addParameter(array('assignerId' => $assignerId,
                    'assignedTime' => date('Y-m-d H:i:s'),
                    'assignStartTime' => $startTime,
                    'assignEndTime' => $endTime,
                    'industryId' => $industryId))
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
        return $this;
    }

    public function deleteIndustry($industry, $transaction = NULL) {
        $industries = is_array($industry) ? $industry : array($industry);
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}subject_industry')
                ->where()
                ->eq('subject_id', ':subjectId:i')
                ->contain('industry_id', ':industryId:s')
                ->closest()
                ->addParameter('subjectId', $this->getId())
                ->addParameter('industryId', $industries)
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();

        return $this;
    }

    public function assignPoi($poiId, $startTime, $endTime, $assignerId, $transaction = NULL) {
        $this->deletePoi($poiId, $transaction);
        Kd_Data_Factory::getInsertDcl()
                ->into('{t}subject_poi')
                ->value('subject_id', $this->getId())
                ->value('assigner_id', ':assignerId:i')
                ->value('is_assigned', 1)
                ->value('assigned_time', ':assignedTime:s')
                ->value('assign_start_time', ':assignStartTime:s')
                ->value('assign_end_time', ':assignEndTime:s')
                ->value('poi_id', ':poiId:s')
                ->addParameter(array('assignerId' => $assignerId,
                    'assignedTime' => date('Y-m-d H:i:s'),
                    'assignStartTime' => $startTime,
                    'assignEndTime' => $endTime,
                    'poiId' => $poiId))
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
        return $this;
    }

    public function deletePoi($poi, $transaction = NULL) {
        $pois = is_array($poi) ? $poi : array($poi);
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}subject_poi')
                ->where()
                ->eq('subject_id', ':subjectId:i')
                ->contain('poi_id', ':poiId:s')
                ->closest()
                ->addParameter('subjectId', $this->getId())
                ->addParameter('poiId', $pois)
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();

        return $this;
    }

    public function associateArticle($data,$status,$transaction = NULL) {
        Kd_Data_Factory::getInsertDcl()
                    ->into('{t}subject_article')
                    ->value('subject_id', $this->getId())
                    ->value('article_id', $data->getId())
                    ->value('is_recommend',$data->getIsRecommend())
                    ->value('is_top', $data['subject_is_top'])
                    ->value('status',':status:s')
                    ->addParameter('status',$status)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function updateArticleRelation($data,$status,$transaction = NULL){
        Kd_Data_Factory::getUpdateDcl()
                    ->into('{t}subject_article')
                    ->value('is_recommend',$data['is_recommend'])
                    ->value('is_top', $data['subject_is_top'])
                    ->eq('article_id',$data['id'])
                    ->eq('subject_id',$this->getId())
                    ->value('status',':status:s')
                    ->addParameter('status',$status)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function deleteArticleRelation($article_id,$transaction = NULL){
        Kd_Data_Factory::getDeleteDcl()
                    ->from('{t}subject_article')
                    ->contain('article_id',':article_id:i')
                    ->eq('subject_id',$this->getId())
                    ->addParameter('article_id',$article_id)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function penndingArticleRelation($status,$ids,$transaction = NULL){
        Kd_Data_Factory::getUpdateDcl()
                    ->into('{t}subject_article')
                    ->value('status',':status:s')
                    ->contain('article_id',':article_id:i')
                    ->eq('subject_id',$this->getId())
                    ->addParameter('status',$status)
                    ->addParameter('article_id',$ids)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function associateConcessions($data, $transaction = NULL) {
        Kd_Data_Factory::getInsertDcl()
                ->into('{t}subject_concessions')
                ->value('subject_id', $this->getId())
                ->value('concessions_id', $data->getId())
                ->value('is_recommend',$data->getIsRecommend())
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function updateConcessionsRelation($data,$transaction = NULL){
        Kd_Data_Factory::getUpdateDcl()
                    ->into('{t}subject_concessions')
                    ->value('is_recommend',$data['is_recommend'])
                    ->eq('concessions_id',$data['id'])
                    ->eq('subject_id',$this->getId())
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function deleteConcessionsRelation($concessions_id,$transaction = NULL){
        Kd_Data_Factory::getDeleteDcl()
                    ->from('{t}subject_concessions')
                    ->contain('concessions_id',':concessions_id:i')
                    ->eq('subject_id',$this->getId())
                    ->addParameter('concessions_id',$concessions_id)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function penndingConcessionsRelation($status,$ids,$transaction = NULL){
        Kd_Data_Factory::getUpdateDcl()
                    ->into('{t}subject_concessions')
                    ->value('status',':status:s')
                    ->contain('concessions_id',':concessions_id:i')
                    ->eq('subject_id',$this->getId())
                    ->addParameter('status',$status)
                    ->addParameter('concessions_id',$ids)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
    }

    public function assignAccount($accountIds, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}subject_account')
                ->where()
                ->eq('subject_id', ':subjectId:i')
                ->closest()
                ->addParameter('subjectId', $this->getId())
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();

        foreach ($accountIds as $accountId) {
            Kd_Data_Factory::getInsertDcl()
                    ->into('{t}subject_account')
                    ->value('subject_id', $this->getId())
                    ->value('account_id', ':accountId:i')
                    ->addParameter('accountId', $accountId)
                    ->setDbDriver($this->getDbDriver($transaction))
                    ->execute();
        }
        return $this;
    }

    public function getIndustries() {
        return App::getModel('foundation/collection_ritu_industry')
                ->fields('a.*', 'f.industry_id', 'f.assign_start_time', 'f.assign_end_time', 'f.assigner_id', 'f.assigned_time', 'f.subject_id')
                ->innerJoin('{t}subject_industry', 'f', 'f.industry_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getRoads() {
        return App::getModel('foundation/collection_road')
                ->fields('a.*', 'f.road_id', 'f.assign_start_time', 'f.assign_end_time', 'f.assigner_id', 'f.assigned_time', 'f.subject_id')
                ->innerJoin('{t}subject_road', 'f', 'f.road_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getPois() {
        return App::getModel('foundation/collection_poi')
                ->fields('a.*', 'f.poi_id', 'f.assign_start_time', 'f.assign_end_time', 'f.assigner_id', 'f.assigned_time', 'f.subject_id')
                ->innerJoin('{t}subject_poi', 'f', 'f.poi_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getConcessions() {
        return App::getModel('promotion/collection_concessions')
                ->fields('a.*', 'f.concessions_id', 'f.assign_start_time', 'f.assign_end_time', 'f.assigner_id', 'f.assigned_time', 'f.subject_id')
                ->field('f.status','subject_status')
                ->innerJoin('{t}subject_concessions', 'f', 'f.concessions_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getArticles() {
        return App::getModel('article/collection_content')
                ->fields('a.*', 'f.article_id', 'f.is_recommend', 'f.is_top','f.subject_id')
                ->field('f.status','subject_status')
                ->innerJoin('{t}subject_article', 'f', 'f.article_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getVideos() {
        return App::getModel('media/collection_video')
                ->innerJoin('{t}subject_video', 'f', 'f.video_id', 'a.id')
                ->eq('f.subject_id', ':subjectId:i')
                ->addParameter('subjectId', $this->getId());
    }

    public function getCategory() {
        if ($this->issetData('category_id')) {
            $this->data['category'] = App::getModel('subject/category')->load($this->getCategoryId());
        }
        return $this->data['category'];
    }

    public function getFirstPicture() {
        if ($this->issetData('picture')) {
            $arr = explode(',', $this->getPicture());
            return $arr[0];
        }
        return '';
    }

    protected function initializeTable($table) {
        $table->setName('{t}subject_content');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('name', 'string');
        $table->createColumn('detail', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('category_id', 'integer');
        $table->createColumn('click_count', 'integer')->setDefaultValue(0);
        $table->createColumn('end_time', 'datetime');
        $table->createColumn('start_time', 'datetime');
        $table->createColumn('is_hot', 'integer');
        $table->createColumn('is_new', 'integer');
        $table->createColumn('is_top', 'integer');
        $table->createColumn('introduction', 'string');
        $table->createColumn('picture', 'string');
        $table->createColumn('template', 'string');
        $table->createColumn('theme_id', 'guid');
        parent::initializeTable($table);
    }

}